package no.monsen.client.controller;

import java.util.ArrayList;
import java.util.List;

import no.monsen.client.ui.MonsenObserver;
import no.monsen.monsenservice.MonsenFact;


public class MonsenMessageList implements MonsenObservable {
	
	private List<MonsenObserver> observers = null;
	private MonsenFact message = null;
	private String severity = null;
	private static MonsenMessageList me = null;

	public MonsenMessageList() {
		observers = new ArrayList<MonsenObserver>();
	}
	
//	public static MonsenMessageList getInstance(){
//		if ( me == null ){
//			return new MonsenMessageList();
//		}
//		return me;
//	}
	/**
	 * Notify all registered observers
	 */
	public void notifyObservers() {
		for (MonsenObserver  obs : observers ) {
			obs.update ( message, severity );
		}
	}
	/**
	 * Register a new observer 
	 */
	public void registerObserver(MonsenObserver observer) {
		if ( observers != null ){
			observers.add( observer );
		}
	}
	/**
	 * Remove a registered observer
	 */
	public void removeObserver(MonsenObserver observer) {
		if ( observers != null ) {
			int index = observers.indexOf( observer );
			if ( index >= 0 ) {
				observers.remove( index );
			}
		}
	}
	
	private void newMessagePosted(){
		notifyObservers();
	}
	
	public void postMessage ( MonsenFact message, String severity ) {
		this.message = message;
		this.severity = severity;
		newMessagePosted();
	}
}
